home *** CD-ROM | disk | FTP | other *** search
/ Power Programmierung / Power-Programmierung (Tewi)(1994).iso / magazine / c_news / 02 / cnews002.nws
Text File  |  1988-06-22  |  18KB  |  410 lines

  1.      Volume 1, Number  2                               10 January 1988
  2.      +---------------------------------------------------------------+
  3.      |                                                               |
  4.      |                                                               |
  5.      |                                                               |
  6.      |                       -  C   News  -                          |
  7.      |                                                               |
  8.      |                                                               |
  9.      |                        International                          |
  10.      |                C Programming & Compiler Review                |
  11.      |                         Newsletter                            |
  12.      |                                                               |
  13.      |                                                               |
  14.      |                                                               |
  15.      +---------------------------------------------------------------+
  16.      US Office:
  17.      Editor                                                Barry Lynch
  18.                                                            Reston, VA
  19.      Foreign Office:     
  20.      Editor                                               David Nugent
  21.                               Melbourne, Australia
  22.  
  23.      C News  is  published  once a month  by  the  C BBS as its official
  24.      newsletter.  You are encouraged to submit articles for publication
  25.      in C News.  Articles should be related to C programming and can be
  26.      Tutorials, reviews or articles of interest to the C programming
  27.      community.  All Operating systems are fairly represented and this
  28.      newsletter shows no favoritism to any one in particular.
  29.      
  30.      C News is the property of the C BBS and is Copyright 1988 by the
  31.      the C BBS.  All rights are reserved and distribution is limited to
  32.      electronic distribution and personal printed copies.  C News cannot
  33.      be resold at any profit, by any organization.  All material enclosed
  34.      within the newsletter is the opinions of the writers and not the
  35.      C BBS or it's Sysop. 
  36.      
  37.                              Table of Contents
  38.  
  39.      1. EDITORIAL 
  40.              The Heap: messages from the editor....................  1
  41.  
  42.      2. BOOK REVIEWS 
  43.         C: The Complete Reference
  44.             reviewed by Tom Leonard  ..............................  2
  45.         TurboC Programmer's Reference Book 
  46.             reviewed by Tom Leonard  ..............................  2
  47.         Crafting C Tools for the IBM PC.
  48.             reviewed by Mike Shaw  ................................  3
  49.  
  50.      3. ARTICLES  
  51.         TurboC & TSR's: A few Questions
  52.             by Arnie Cherdak  ......... ...........................  4                     
  53.  
  54.            C News 1-02                 Page 1                   10 Jan 1988
  55.  
  56.  
  57.      =================================================================
  58.                                  EDITORIAL
  59.      =================================================================
  60.  
  61.      The HEAP: Messages from the Editor.
  62.  
  63.  
  64.           Well with only one issue under my belt, the C News -letter is
  65.      already expanding.  David Nugent has voluntered to be a contributing
  66.      editor for future issues of C News.  Since David is located in
  67.      Melbourne, Australia I figured that he could head the foreign office
  68.      there for us.  Gee, the C BBS is now international in scope.  
  69.  
  70.              Besides, David other C BBS users are pitching in to contribute
  71.      reviews, tutorials and other information to this newsletter.  Three
  72.      users as of this issue have contributed, with others planning to in
  73.      future issues.  Keep in mind that this Newletters letter is for all
  74.      C programmers, and I hope that you will send in questions, articles,
  75.      book reviews and sample programs that you might like to show off.
  76.  
  77.           Each article has the author's name included in the byline.  Any
  78.      comments on any of the articles should be sent to the those individuals,
  79.      direct if you would    like, or to the C BBS.  
  80.  
  81.           I started an article on the "Legal Aspects of Software Design",
  82.      but decided to get some legal counsel before I put anything on paper.
  83.      Never fear that article will be finished by the next issue.  Speaking
  84.      of which the next issue is scheduled for publication two weeks from 
  85.      today - 17 January.  If you would like to submit an article to C News,
  86.      it must be here by the 14th to be included.   Thanks
  87.  
  88.  
  89.      B C'ing U..
  90.  
  91.      Barry
  92.                        
  93.            C News 1-02                 Page 2                   10 Jan 1988
  94.  
  95.      ======================================================================
  96.                                 BOOK REVIEWS
  97.      ======================================================================
  98.  
  99.      ______________________________________________________________________ 
  100.  
  101.      Title:     C: The Complete Reference 
  102.      Author/s:  Herbert Schildt 
  103.      Publisher: Osborne McGraw-Hill 
  104.                 2600 Tenth Street 
  105.                 Berkeley, California 94710 
  106.      ISBN #:    0-07-881263-1 
  107.  
  108.            If you are just starting into the C langugage you should get a 
  109.      copy of this book.  This book tries to be the COMPLETE REFERENCE TO C 
  110.      and to a large extent it is.  When I need to known how to use a  
  111.      multidimensional array  or when I want a library routine that will  
  112.      copy a string  this is the book I look in first.
  113.  
  114.           "C: The Complete Reference" is a large book.  It is over 700 pages 
  115.      and it will probably take you a few weeks to read through it but it is 
  116.      worth the effort. 
  117.  
  118.      _______________________________________________________________________ 
  119.  
  120.      Title:     Turbo C  Programmer's Resource Book 
  121.      Author/s:  Fredrick Holtz 
  122.      Publisher: TAB books Inc 
  123.                 P.O. Box 40 
  124.                 Blue Ridge Summit, PA 17214 
  125.      ISBN #:    0-8306-3030-9 
  126.  
  127.             If you are not sure of the differents between a string and a 
  128.      character array  or if the following line of codes makes your eyes cross 
  129.  
  130.        int x=0; 
  131.        while((a[x] += (a[x]>='A' && a[x]<='Z') ? ' ':0)) ++x; 
  132.  
  133.      Then this books should help quite a bit.  While the title says TURBO C 
  134.      the book will be of value with any C compiler.  It very gently and 
  135.      carefully explains the C languages.  This is not a complete reference 
  136.      to C but is an excellent tutorial for someone just starting into the 
  137.      languages. 
  138.  
  139.      Hint:  The above code converts the lower case letters in a string to 
  140.             upper case letters. 
  141.  
  142.            C News 1-02                 Page 3                   10 Jan 1988
  143.  
  144.      ======================================================================
  145.                                 BOOK REVIEWS
  146.      ======================================================================
  147.  
  148.      ______________________________________________________________________
  149.     
  150.      Title:     Crafting C Tools for the IBM PCs
  151.      Author:    Joe Campbell
  152.      Publisher: Prentice-Hall Software Series
  153.      ISBN #:    0-13-188418-2
  154.                  
  155.       This book is an excellent one to read if you want to understand  the 
  156.       internal workings of the 8086/8088 processor and MS-DOS.  The author 
  157.       provides valuable insites into how programs are loaded and  executed 
  158.       by DOS and how Assembler-to-C module communication occurs.  Sections 
  159.       are  included  on  directory  structure,  keyboard  input  handling, 
  160.       standard  output as well as direct video access,  and advanced video 
  161.       topics such as coding window subroutines.  The last  topics  covered 
  162.       include  coding  your  own  interrupt  handling  routine and  serial 
  163.       communication processor.  
  164.  
  165.       Despite its title, this book is almost one-third Assembler routines.  
  166.       Many Assembler and C routine examples are included; the examples are 
  167.       built   up   chapter-to-chapter,    eventually   resulting   in    a 
  168.       sophisticated directory listing program.   
  169.  
  170.       A  section  is  included  on  C  compiler differences,  memory model 
  171.       considerations for C programs,  and the  complete  8086  instruction 
  172.       set.  
  173.  
  174.       I recommend this book to anyone who wants to use C and Assembler  to 
  175.       write DOS utility programs.  
  176.  
  177.       _____________________________________________________________________
  178.  
  179.     
  180.  
  181.            C News 1-02                 Page 4                   10 Jan 1988
  182.  
  183.      ======================================================================
  184.      TurboC and TSR's by Arnie Cherdak..
  185.      ======================================================================
  186.  
  187.  
  188.      It is unfortunate that Borland's documentation of their INTERRUPT type
  189.      function is rather sketchy.  Certain features appear to be a mystery
  190.      and I wanted to share some of the insights I have had.  My problem
  191.      related to the code generated by the Turbo C compilers.
  192.  
  193.      INTRODUCTION
  194.  
  195.      An interrupt service routine (ISR) terminates with an "iret" assembly
  196.      language instruction.  The difference between this and a regular "ret"
  197.      instruction used to return from a regular function call is shown below.
  198.  
  199.      CPU Action         Instructions>>> ret    far ret      iret
  200.       pop instruction pointer value     YES       Y           Y
  201.       pop code segment value            NO        Y           Y
  202.       pop flags                         N         N           Y
  203.  
  204.      Functions compiled in the Tiny, Small, and Compact models would use
  205.      the "ret" instruction since they have <= 64K of code.  Medium, Large,
  206.      and Huge model functions would use the far ret instruction to allow
  207.      function calls beyond the 64K limit.
  208.  
  209.      The values on the stack get there through the normal action brought
  210.      about by the instructions used to get to the functions.  A simple call
  211.      within a 64K segment causes the instruction pointer to be pushed onto
  212.      the stack while a far call causes the code segment value to be added.
  213.      Indirect calls are a bit different but result in the same stack
  214.      configuration.  An interrupt service routine is executed through an
  215.      "int" instruction which causes the flags, segment, and instruction
  216.      pointer to be pushed on the stack before the CPU is sent off to execute
  217.      the routine.
  218.  
  219.      OBSERVATION
  220.  
  221.      I recently bought Al Stevens' book, "Turbo C -- Memory-Resident
  222.      Utilities, Screen I/O and Programming Techniques."  In my opinion,
  223.      he has written an exceptionally clear and understandable text and
  224.      has produced a fine bit of programming that works extremely well.
  225.      He has my congratulations on a job well done.
  226.  
  227.      In going through Stevens' code for interrupt type functions, I saw
  228.      that he invokes ISR's within those functions with simple function
  229.      calls and apparently without accomodation for the iret instruction
  230.      that must appear at the exit point of the ISR.  An example:
  231.  
  232.            C News 1-02                 Page 5                   10 Jan 1988
  233.  
  234.      ======================================================================
  235.      TurboC and TSR's by Arnie Cherdak..
  236.      ======================================================================
  237.  
  238.      /***************************************************************
  239.  
  240.       QUEST.C -- Test program to examine ISR return code
  241.  
  242.      ****************************************************************/
  243.  
  244.      #include <stdio.h>
  245.      #define  intnum 0x16
  246.  
  247.  
  248.      void interrupt (*oldisr)(); /* prototype and declaration of oldisr */
  249.                               /* which is a variable declared as a   */
  250.                               /* pointer to an interrupt function.   */
  251.  
  252.  
  253.      void interrupt newisr()   /* new isr is executed each time      */
  254.      {                         /* interrupt intnum is executed       */
  255.            (*oldisr)();
  256.      }
  257.  
  258.      main()                 /* saves old isr vector & installs new */
  259.      {
  260.           oldisr = getvect(intnum);
  261.           setvect(intnum,newisr);
  262.      }
  263.  
  264.      /******************** End of Quest.c **************************/
  265.  
  266.      Here, the value of the vector for interrupt, intnum, is retrieved and
  267.      saved and it is then replaced with a vector pointing to the new isr,
  268.      "newisr." OK so far!
  269.  
  270.      Here's where my understanding goes to pot.  The new isr calls the old
  271.      isr which is a construct Stevens uses quite frequently although there
  272.      is generally other    code within the new isr as might be expected.  I left
  273.      it out here to simplify matters.  Now regardless which memory model is
  274.      applied here, it seems that the function call to oldisr should result
  275.      in a far call instruction being pushed onto the stack by Turbo C and,
  276.      as I know from my travels through the BIOS    listings, oldisr invariably
  277.      returns with an iret.  It seems that the thing should blow up with the
  278.      stack underflowing almost immediately but it doesn't. It works just
  279.      fine.
  280.  
  281.            C News 1-02                 Page 6                   10 Jan 1988
  282.      ======================================================================
  283.      TurboC and TSR's by Arnie Cherdak..
  284.      ======================================================================
  285.  
  286.      Obviously, I missed something.  The Turbo C manuals don't clarify this
  287.      or even seem to mention it.  TC must make an accomodation for the type
  288.      of the called isr vector but I was baffled.  Anybody have any insights
  289.      to share?
  290.  
  291.      I'm going to compile the above example using the TCC switch to yield
  292.      an    assembly language output.  I should be able to see what's happening
  293.      from that.
  294.  
  295.      /***************** TIME PASSES *********************************/
  296.  
  297.      THE ANSWER
  298.  
  299.      Well, I used the "-S" switch on TCC, the command line compiler, and I
  300.      got my answer.  As you can see from the resulting assembly language
  301.      output below, Turbo C inserts a "pushf" instruction ahead of the call
  302.      to "oldisr" which causes the flags to be pushed onto the stack.  Next,
  303.      notice that the call to oldisr is an indirect call using the value of
  304.      the vector to oldisr stored in memory.  In effect, Turbo C has
  305.      circumvented the effects of the compilation memory model type by
  306.      using the pushf and the indirect call which created a stack configuration
  307.      exactly like that produced by an "int" instruction and allowed the "iret"
  308.      in the oldisr to execute without bombing the system.
  309.  
  310.                            MYSTERY SOLVED!!!
  311.  
  312.      ;=====================================================================
  313.      ;
  314.      ;      ASSEMBLY LANGUAGE OUTPUT FROM TCC FOR COMPILATION OF
  315.      ;      ISR ROUTINE, QUEST.C
  316.      ;
  317.      ;=====================================================================
  318.                 name    quest
  319.      _TEXT   segment byte public 'CODE'
  320.      DGROUP  group   _DATA,_BSS
  321.              assume  cs:_TEXT,ds:DGROUP,ss:DGROUP
  322.      _TEXT   ends
  323.      _DATA   segment word public 'DATA'
  324.      _d@     label   byte
  325.      _DATA   ends
  326.      _BSS    segment word public 'BSS'
  327.      _b@     label   byte
  328.      _BSS    ends
  329.      _TEXT   segment byte public 'CODE'
  330.  
  331.      ; Line 11
  332.      _newisr proc    far
  333.              push    ax
  334.              push    bx
  335.              push    cx
  336.              push    dx
  337.              push    es
  338.              push    ds
  339.              push    si
  340.              push    di
  341.              push    bp
  342.              mov     ds,cs:DGROUP@
  343.  
  344.            C News 1-02                 Page 7                   10 Jan 1988
  345.  
  346.      ======================================================================
  347.      TurboC and TSR's by Arnie Cherdak..
  348.      ======================================================================
  349.  
  350.  
  351.      ; Line 12
  352.              pushf                    ; <============== LOOK HERE!!!!!
  353.         call    dword ptr DGROUP:_oldisr  ; <============== Turbo C Did It!
  354.      ; Line 13
  355.      @1:
  356.             pop     bp
  357.             pop     di
  358.             pop     si
  359.             pop     ds
  360.             pop     es
  361.             pop     dx
  362.             pop     cx
  363.             pop     bx
  364.             pop     ax
  365.             iret    
  366.             _newisr endp
  367.  
  368.       ; Line 16
  369.        _main   proc    near
  370.       ; Line 17
  371.         mov     ax,22
  372.         push    ax
  373.         call    near ptr _getvect
  374.         pop     cx
  375.         cwd     
  376.         mov     word ptr DGROUP:_oldisr+2,dx
  377.         mov     word ptr DGROUP:_oldisr,ax
  378.       ; Line 18
  379.         mov     ax,offset _newisr
  380.         mov     dx,cs
  381.         push    dx
  382.         push    ax
  383.         mov     ax,22
  384.         push    ax
  385.         call    near ptr _setvect
  386.         add     sp,6
  387.       ; Line 19
  388.       @2:
  389.         ret     
  390.         _main   endp
  391.         _TEXT   ends
  392.         _BSS    segment word public 'BSS'
  393.                 public  _oldisr
  394.         _oldisr label   dword
  395.                 db      4 dup (?)
  396.         _BSS    ends
  397.         _DATA   segment word public 'DATA'
  398.         _s@     label   byte
  399.         _DATA   ends
  400.         _TEXT   segment byte public 'CODE'
  401.                 extrn   DGROUP@:word
  402.                 extrn   _setvect:near
  403.                 public  _newisr
  404.                 public  _main
  405.                 extrn   _getvect:near
  406.          _TEXT   ends
  407.                  end
  408.  
  409.     
  410.